# coding=utf-8

import datetime
import logging
import traceback

import psutil
import requests
import sys
import json

reload(sys)
sys.setdefaultencoding('utf-8')

import signal
import time

print psutil.virtual_memory()

longvalue = float("4,506,112.00".replace(",", ""))

paramLink = 'securityCode=600000.SH&holderCode=80188285&holderName=中国证券金融股份有限公司&endDate=2018-03-31'

print paramLink.split("=")[len(paramLink.split("="))-1]

print longvalue

class GracefulKiller:

    def __init__(self):
        signal.signal(signal.SIGINT, self.exit_gracefully)
        signal.signal(signal.SIGTERM, self.exit_gracefully)

    def exit_gracefully(self, signum, frame):
        print("python killed: signum:" + str(signum) + ", frame:" + str(frame))
        traceback.print_stack(frame)


killer = GracefulKiller()

print('2018-02-04'.replace("-", ""))
nan = None
zero = 0
false = False
empty_str = ''
empty_list = []
empty_tuple = ()
empty_dist = {'a': 1}
if not nan:
    print "---- not nan is true"
if not zero:
    print "---- not 0 is true"
if not false:
    print "---- not False is true"
if not empty_str:
    print "---- not empty string( '')  is true"
if not empty_list:
    print "---- not empty list([])  is true"
if not empty_tuple:
    print "---- not empty tuple(())  is true"
if not empty_dist:
    print "---- not empty dict({})  is true"

#################test##############################
print("he".zfill(4))
s = '我是一串汉字'
print(unicode("何").startswith("何"))
s = 'heminhang'
s2 = u'heminhang'
if (s == s2):
    print('s == s2')

content = "a,b,c,d\na2,b2,b2,d2"
lines = content.split("\n")
lines = lines[1:]
print(len(lines))
print(len(lines))

arra = [1, 3, 5];
arra = arra[1:]
arra.reverse()
print(arra)

string = 'abcd'
t = (1, 2, 3, 4)
print list(t)[1: -0]

print (datetime.datetime.strptime('1985-1-2', "%Y-%m-%d") + datetime.timedelta(days=1)).strftime("%Y-%m-%d")

arr = list("abcd")
print arr.index('a')
empty_string = 0;
##print(len(empty_string))
if not empty_string:
    print("empty")
print(range(2))
line = 'zichan\thello'
print(len(line.split("\t")))

print(filter(str.isdigit, str(u'123456a')))

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
handler = logging.FileHandler('hello.log')
handler.setLevel(logging.INFO)
logger.addHandler(handler)
data = {"报表日期": 'heminhang'}
print(data['报表日期'])


def mtest():
    print ("mtest method in hmh")


logger.info("this is from loggin")

dist = {"heminhang": ''}
print(len(dist.keys()))

dict1 = {"name": 'hemimhang'}
dict2 = {"name2": 'heminhang2'}
dict1.update(dict2)
print(str(dict1))
print(dict(dict1.items() + dict2.items()))
empty_dict = {}
print(len(empty_dict.items()))
print(bool(empty_dict))

#time.sleep(13.5)

import time

'''
{
report: "2018年中报",
records: [
{
    SHAREHDNAME: "宜宾市国有资产经营有限公司",
    SHAREHDNATURE: "其它",
    SHAREHDNUM: "1,366,548,020",
    SHAREHDNUMPERG: "35.21",
    SHAREHDNUMPERG_ChartValue: "35.21",
    SHAREHDNUMZLTG: "36.00",
    SHAREHDNUM_CHANGE2: "不变",
    SHAREHDNUM_CHANGE2_ChartValue: "0.00",
    SHAREHDNUMPER_CHANGE: "-0.79",
    SHARETYPE: "A股",
    DIRECTION: "不变",
    SECURITYCODE: "000858.SZ",
    SHAREHDCODE: "80002426",
    ENDDATE: "2018-06-30",
    LinkPara: "securitycode=000858.SZ&holderCode=80002426&holdername=宜宾市国有资产经营有限公司&endDate=2018-06-30"
    },
    {
    SHAREHDNAME: "瑞士联合银行集团",
    SHAREHDNATURE: "QFII",
    SHAREHDNUM: "29,995,997",
    SHAREHDNUMPERG: "0.79",
    SHAREHDNUMPERG_ChartValue: "0.79",
    SHAREHDNUMZLTG: "0.79",
    SHAREHDNUM_CHANGE2: "4,506,112",
    SHAREHDNUM_CHANGE2_ChartValue: "4,506,112.00",
    SHAREHDNUMPER_CHANGE: "0.12",
    SHARETYPE: "A股",
    DIRECTION: "增持",
    SECURITYCODE: "000858.SZ",
    SHAREHDCODE: "80044863",
    ENDDATE: "2017-03-31",
    LinkPara: "securitycode=000858.SZ&holderCode=80044863&holdername=瑞士联合银行集团&endDate=2017-03-31"
    },
    {
        "SHAREHDNAME": "合计",
        "SHAREHDNATURE": "",
        "SHAREHDNUM": "330,637,802",
        "SHAREHDNUMPERG": "63.58",
        "SHAREHDNUMPERG_ChartValue": "",
        "SHAREHDNUMZLTG": "63.88",
        "SHAREHDNUM_CHANGE2": "",
        "SHAREHDNUM_CHANGE2_ChartValue": "",
        "SHAREHDNUMPER_CHANGE": "",
        "SHARETYPE": "",
        "DIRECTION": "",
        "SECURITYCODE": "",
        "SHAREHDCODE": "",
        "ENDDATE": "",
        "LinkPara": ""
      }
'''
i = 0
print "Start : %s" % time.ctime()


def transfer_to_period(period):
    period = period.encode("utf-8")
    '''
    period -> 2018年中报
    period -> 2005-10-23(股改前)
    '''
    '''
    2017年年报 -> 2017-12-31
    2017年三季报 -> 2017-09-30
    2017年中报 -> 2017-06-30
    2017年一季报 -> 2017-03-31
    '''
    map = {
        '年年报': '12-31',
        '年三季报': '09-30',
        '年中报': '06-30',
        '年一季报': '03-31',
    }
    year = period[0:4]
    key = period[4:]
    if map.has_key(key):
        month_day = map[key]
        return year + "-" + month_day;
    else:
        print period
        return period[0: 10]

def downoad_parse_top10circulation_shareholder(stock_code):

    stock_suffix = ".SZ"  ##创业板和深圳中小板
    if stock_code.startswith("6"):  ##沪市主板
        stock_suffix = ".SH"
    url = "http://app2.jg.eastmoney.com/stock/f9/GetTop10CirculationShareHolder?securityCode=" + stock_code + stock_suffix + "&startDate=2006-01-01&endDate=2018-12-31&order=desc"
    response = requests.request("get", url, headers= {"user-agent": "Mozilladdddd"});
    response_content = response.content.decode("UTF-8");
    period_data_list = json.loads(response_content)
    for period_data in period_data_list:
        records = []
        period = transfer_to_period(period_data['report'])
        for r in period_data['records']:
            if r['SHAREHDNAME'] == '合计':
                total = {}
                total['code'] = stock_code
                total['period'] = period
                total['num'] = long(float(r['SHAREHDNUM'].replace(',', '')))
                total['num_pct'] = float(r['SHAREHDNUMPERG'])
            else:
                record = {}
                record['code'] = stock_code
                record['period'] = period
                record['category'] = 'Top10Circulation'
                record['name'] = r['SHAREHDNAME']
                record['nature'] = r['SHAREHDNATURE']
                record['num'] = long(float(r['SHAREHDNUM'].replace(',', '')))
                # like '66,032,468'， 当没有变化的时候 '不变'
                if r['SHAREHDNUM_CHANGE2'] == '不变':
                    record['num_change'] = long(0)
                elif r['SHAREHDNUM_CHANGE2'] == '新进':
                    record['num_change'] = record['num']

                else:
                    record['num_change'] = long(float(r['SHAREHDNUM_CHANGE2'].replace(',', '')))

                record['num_pct'] = float(r['SHAREHDNUMPERG'])
                record['direction'] = r['DIRECTION']
                record['createTime'] = datetime.datetime.now()
                record['modifyTime'] = datetime.datetime.now()
                records.append(record)

    return records


'''
[
{
Date: "2018年中报",
Details: [
{
SHAREHDNAME: "上海国际集团有限公司",
SHAREHDNATURE: "其它",
SHAREHDNUM: "6,331,322,671",
SHAREHDNUMPER: "21.57",
SHAREHDNUMPER_ChartValue: "21.57",
SHAREHDNUM_CHANGE: "不变",
SHAREHDNUM_CHANGE_ChartValue: "0.00",
SHAREHDNUMPER_CHANGE: "0.00",
SHARETYPE: "流通A股,限售流通A股",
DIRECTION: "不变",
LinkPara: "securityCode=600000.SH&holderCode=80008357&holderName=上海国际集团有限公司&endDate=2018-06-30"
},
{
SHAREHDNAME: "中国证券金融股份有限公司",
SHAREHDNATURE: "其它",
SHAREHDNUM: "1,283,011,919",
SHAREHDNUMPER: "4.37",
SHAREHDNUMPER_ChartValue: "4.37",
SHAREHDNUM_CHANGE: "66,032,468",
SHAREHDNUM_CHANGE_ChartValue: "66,032,468.00",
SHAREHDNUMPER_CHANGE: "0.22",
SHARETYPE: "流通A股",
DIRECTION: "增持",
LinkPara: "securityCode=600000.SH&holderCode=80188285&holderName=中国证券金融股份有限公司&endDate=2018-03-31"
},
{
        "SHAREHDNAME": "合计",
        "SHAREHDNATURE": "",
        "SHAREHDNUM": "22,388,118,425",
        "SHAREHDNUMPER": "76.28",
        "SHAREHDNUMPER_ChartValue": "",
        "SHAREHDNUM_CHANGE": "",
        "SHAREHDNUM_CHANGE_ChartValue": "",
        "SHAREHDNUMPER_CHANGE": "",
        "SHARETYPE": "",
        "DIRECTION": "",
        "LinkPara": ""
      }
'''
def downoad_parse_top10_shareholder(stock_code):
    stock_suffix = ".SZ"  ##创业板和深圳中小板
    if stock_code.startswith("6"):  ##沪市主板
        stock_suffix = ".SH"
    url= "http://app2.jg.eastmoney.com/stock/F9/GetTop10HolderList?securityCode="+stock_code + stock_suffix +"&startDate=1900-01-01&endDate=2018-12-31&order=desc"
    response = requests.request("get", url, headers= {"user-agent": "Mozilladdddd"});
    response_content = response.content.decode("UTF-8");
    period_data_list = json.loads(response_content)
    records = []
    totals = []
    for period_data in period_data_list:
        period_str = period_data['Date']
        period = transfer_to_period(period_str)
        for r in period_data['Details']:
            if r['SHAREHDNAME'] == '合计':
                total = {}
                total['code'] = stock_code
                total['period'] = period
                total['num'] = long(float(r['SHAREHDNUM'].replace(',', '')))
                total['num_pct'] = float(r['SHAREHDNUMPER'])
                totals.append(total)
            else:
                record = {}
                record['code'] = stock_code
                record['period'] = period
                record['category'] = 'Top10'
                record['name'] = r['SHAREHDNAME']
                record['nature'] = r['SHAREHDNATURE']
                record['num'] = long(float(r['SHAREHDNUM'].replace(',', '')))
                # like '66,032,468'， 当没有变化的时候 '不变'
                if r['SHAREHDNUM_CHANGE'] == '不变' :
                    record['num_change'] = long(0)
                elif r['SHAREHDNUM_CHANGE'] == '新进' :
                    record['num_change'] = record['num']
                else :
                    record['num_change'] = long(float(r['SHAREHDNUM_CHANGE'].replace(',', '')))
                record['num_pct'] = float(r['SHAREHDNUMPER'])
                record['direction'] = r['DIRECTION']
                record['createTime'] = datetime.datetime.now()
                record['modifyTime'] = datetime.datetime.now()
                records.append(record)
    return records

records = downoad_parse_top10circulation_shareholder('600004')
for record in records:
    print record['period']

records = downoad_parse_top10_shareholder('600004')
for record in records:
    print record['period']

print(transfer_to_period('2018年中报'))